home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / othergnu / indent~1.zoo / backup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  8.5 KB  |  343 lines

  1.  
  2. /* backup.c -- make Emacs style backup file names
  3.    Copyright (C) 1992 Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it without restriction.
  7.  
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11.  
  12.  
  13.  
  14.    GNU/Emacs style backups --
  15.    This behaviour is controlled by two environment variables,
  16.    VERSION_CONTROL and SIMPLE_BACKUP_SUFFIX.
  17.  
  18.    VERSION_CONTROL determines what kinds of backups are made.  If it's
  19.    value is "numbered", then the first modification of some file
  20.    "eraserhead.c" will yield a backup file "eraserhead.c.~1~", the
  21.    second modification will yield "eraserhead.c.~2~", and so on.  It
  22.    does not matter if the version numbers are not a sequence;  the next
  23.    version will be one greater than the highest in that directory.
  24.  
  25.    If the value of VERSION_CONTROL is "numbered_existing", then such
  26.    numbered backups will be made if there are already numbered backup
  27.    versions of the file.  Otherwise, the backup name will be that of
  28.    the original file with "~" (tilde) appended.  E.g., "eraserhead.c~".
  29.  
  30.    If the value of VERSION_CONTROL is "simple", then the backup name
  31.    will be that of the original file with "~" appended, regardless of
  32.    whether or not there exist numbered versions in the directory.
  33.  
  34.    For simple backups, the value of SIMPLE_BACKUP_SUFFIX will be used
  35.    rather than "~" if it is set.
  36.  
  37.    If VERSION_CONTROL is unset, "numbered_existing" is assumed.  For
  38.    Emacs lovers, "nil" is equivalent to "numbered_existing" and "t" is
  39.    equivalent to "numbered".
  40.  
  41.    Finally, if VERSION_CONTROL is "none" or "never", backups are not
  42.    made.  I suggest you avoid this behaviour. */
  43.  
  44. /* Written by jla, based on code from djm (see `patch') */
  45.  
  46. #include "backup.h"
  47. #include "sys.h"
  48. #include <ctype.h>
  49.  
  50. #ifndef isascii
  51. #define ISDIGIT(c) (isdigit ((unsigned char) (c)))
  52. #else
  53. #define ISDIGIT(c) (isascii (c) && isdigit (c))
  54. #endif
  55.  
  56. #ifndef NODIR
  57.  
  58. #include <sys/types.h>
  59.  
  60. #ifdef DIRENT
  61. #include <dirent.h>
  62. #ifdef direct
  63. #undef direct
  64. #endif
  65. #define direct dirent
  66. #define NLENGTH(direct) (strlen((direct)->d_name))
  67. #else /* !DIRENT */
  68. #define NLENGTH(direct) ((direct)->d_namlen)
  69. #ifdef USG
  70. #ifdef SYSNDIR
  71. #include <sys/ndir.h>
  72. #else /* !SYSNDIR */
  73. #include <ndir.h>
  74. #endif /* !SYSNDIR */
  75. #else /* !USG */
  76. #include <sys/dir.h>
  77. #endif /* !USG */
  78. #endif /* !DIRENT */
  79.  
  80. #if defined (HAVE_UNISTD_H)
  81. #include <unistd.h>
  82. #endif
  83.  
  84. #if defined (_POSIX_VERSION)    /* Might be defined in unistd.h.  */
  85. /* POSIX does not require that the d_ino field be present, and some
  86.    systems do not provide it. */
  87. #define REAL_DIR_ENTRY(dp) 1
  88. #else
  89. #define REAL_DIR_ENTRY(dp) ((dp)->d_ino != 0)
  90. #endif
  91.  
  92. #else /* NODIR */
  93. #define generate_backup_filename(v,f) simple_backup_name((f))
  94. #endif /* NODIR */
  95.  
  96. /* Default backup file suffix to use */
  97. char *simple_backup_suffix = "~";
  98.  
  99. /* What kinds of backup files to make -- see
  100.    table `version_control_values' below. */
  101. enum backup_mode version_control = unknown;
  102.  
  103.  
  104. /* Construct a simple backup name for PATHNAME by appending
  105.    the value of `simple_backup_suffix'. */
  106.  
  107. static char *
  108. simple_backup_name (pathname)
  109.      char *pathname;
  110. {
  111.   char *backup_name;
  112.  
  113.   backup_name = xmalloc (strlen (pathname)
  114.              + strlen (simple_backup_suffix) + 2);
  115.   sprintf (backup_name, "%s%s", pathname, simple_backup_suffix);
  116.   return backup_name;
  117. }
  118.  
  119. #ifndef NODIR
  120. /* If DIRENTRY is a numbered backup version of file BASE, return
  121.    that number.  BASE_LENGTH is the string length of BASE. */
  122.  
  123. static int
  124. version_number (base, direntry, base_length)
  125.      char *base;
  126.      char *direntry;
  127.      int base_length;
  128. {
  129.   int version;
  130.   char *p;
  131.  
  132.   version = 0;
  133.   if (!strncmp (base, direntry, base_length)
  134.       && ISDIGIT (direntry[base_length + 2]))
  135.     {
  136.       for (p = &direntry[base_length + 2]; ISDIGIT (*p); ++p)
  137.     version = version * 10 + *p - '0';
  138.       if (p[0] != '~' || p[1])
  139.     version = 0;
  140.     }
  141.  
  142.   return version;
  143. }
  144.  
  145.  
  146. /* Return the highest version of file FILENAME in directory
  147.    DIRNAME.  Return 0 if there are no numbered versions. */
  148.  
  149. static int
  150. highest_version (filename, dirname)
  151.      char *filename, *dirname;
  152. {
  153.   DIR *dirp;
  154.   struct direct *dp;
  155.   int highest_version;
  156.   int this_version;
  157.   int file_name_length;
  158.  
  159.   dirp = opendir (dirname);
  160.   if (!dirp)
  161.     return 0;
  162.  
  163.   highest_version = 0;
  164.   file_name_length = strlen (filename);
  165.  
  166.   while ((dp = readdir (dirp)) != 0)
  167.     {
  168.       if (!REAL_DIR_ENTRY (dp) || NLENGTH (dp) <= file_name_length + 2)
  169.     continue;
  170.  
  171.       this_version = version_number (filename, dp->d_name, file_name_length);
  172.       if (this_version > highest_version)
  173.     highest_version = this_version;
  174.     }
  175.  
  176.   closedir (dirp);
  177.   return highest_version;
  178. }
  179.  
  180.  
  181. /* Return the highest version number for file PATHNAME.  If there
  182.    are no backups, or only a simple backup, return 0. */
  183.  
  184. static int
  185. max_version (pathname)
  186.      char *pathname;
  187. {
  188.   register char *p;
  189.   register char *filename;
  190.   int pathlen = strlen (pathname);
  191.   int version;
  192.  
  193.   p = pathname + pathlen - 1;
  194.   while (p > pathname && *p != '/')
  195.     p--;
  196.  
  197.   if (*p == '/')
  198.     {
  199.       int dirlen = p - pathname;
  200.       register char *dirname;
  201.  
  202.       filename = p + 1;
  203.       dirname = xmalloc (dirlen + 1);
  204.       strncpy (dirname, pathname, (dirlen));
  205.       dirname[dirlen] = '\0';
  206.       version = highest_version (filename, dirname);
  207.       free (dirname);
  208.       return version;
  209.     }
  210.  
  211.   filename = pathname;
  212.   version = highest_version (filename, ".");
  213.   return version;
  214. }
  215.  
  216.  
  217. /* Generate a backup filename for PATHNAME, dependent on the
  218.    value of VERSION_CONTROL. */
  219.  
  220. static char *
  221. generate_backup_filename (version_control, pathname)
  222.      enum backup_mode version_control;
  223.      char *pathname;
  224. {
  225.   int last_numbered_version;
  226.   char *backup_name;
  227.  
  228.   if (version_control == none)
  229.     return 0;
  230.  
  231.   if (version_control == simple)
  232.     return simple_backup_name (pathname);
  233.  
  234.   last_numbered_version = max_version (pathname);
  235.   if (version_control == numbered_existing
  236.       && last_numbered_version == 0)
  237.     return simple_backup_name (pathname);
  238.  
  239.   last_numbered_version++;
  240.   backup_name = xmalloc (strlen (pathname) + 16);
  241.   if (!backup_name)
  242.     return 0;
  243.  
  244.   sprintf (backup_name, "%s.~%d~", pathname, last_numbered_version);
  245.   return backup_name;
  246. }
  247.  
  248. #endif /* !NODIR */
  249.  
  250. static struct version_control_values values[] =
  251. {
  252.   {none, "never"},        /* Don't make backups. */
  253.   {simple, "simple"},        /* Only simple backups */
  254.   {numbered_existing, "existing"},    /* Numbered if they already exist */
  255.   {numbered_existing, "nil"},    /* Ditto */
  256.   {numbered, "numbered"},    /* Numbered backups */
  257.   {numbered, "t"},        /* Ditto */
  258.   {unknown, 0}            /* Initial, undefined value. */
  259. };
  260.  
  261.  
  262. extern char *getenv ();
  263.  
  264. /* Determine the value of `version_control' by looking in the
  265.    environment variable "VERSION_CONTROL".  Defaults to
  266.    numbered_existing. */
  267.  
  268. enum backup_mode
  269. version_control_value ()
  270. {
  271.   char *version;
  272.   struct version_control_values *v;
  273.  
  274.   version = getenv ("VERSION_CONTROL");
  275.   if (version == 0 || *version == 0)
  276.     return numbered_existing;
  277.  
  278.   v = &values[0];
  279.   while (v->name)
  280.     {
  281.       if (strcmp (version, v->name) == 0)
  282.     return v->value;
  283.       v++;
  284.     }
  285.  
  286.   return unknown;
  287. }
  288.  
  289.  
  290. /* Initialize information used in determining backup filenames. */
  291.  
  292. void
  293. initialize_backups ()
  294. {
  295.   char *v = getenv ("SIMPLE_BACKUP_SUFFIX");
  296.  
  297.   if (v && *v)
  298.     simple_backup_suffix = v;
  299. #ifdef NODIR
  300.   version_control = simple;
  301. #else /* !NODIR */
  302.   version_control = version_control_value ();
  303.   if (version_control == unknown)
  304.     {
  305.       fprintf (stderr, "indent:  Strange version-control value\n");
  306.       fprintf (stderr, "indent:  Using numbered-existing\n");
  307.       version_control = numbered_existing;
  308.     }
  309. #endif /* !NODIR */
  310. }
  311.  
  312. /* Prints an error message using `perror' */
  313. extern void sys_error ();
  314.  
  315. /* Make a backup copy of FILE, taking into account version-control.
  316.    See the description at the beginning of the file for details. */
  317.  
  318. void
  319. make_backup (file)
  320.      struct file_buffer *file;
  321. {
  322.   int fd;
  323.   register char *p = file->name + strlen (file->name) - 1;
  324.   char *backup_filename;
  325.   char *new_backup_name;
  326.  
  327.   backup_filename = generate_backup_filename (version_control, file->name);
  328.   if (!backup_filename)
  329.     {
  330.       fprintf (stderr, "indent: Can't make backup filename of %s", file->name);
  331.       exit (1);
  332.     }
  333.  
  334.   fd = creat (backup_filename, 0666);
  335.   if (fd < 0)
  336.     sys_error (backup_filename);
  337.   if (write (fd, file->data, file->size) != file->size)
  338.     sys_error (backup_filename);
  339.  
  340.   close (fd);
  341.   free (backup_filename);
  342. }
  343.